#include"Render/Texture.hpp"
#include"Render/stb_image.h"
 
Texture::Texture(std::string& path)
:myRendererID(0),myFilePath(path),myLocalBuffer(nullptr),myWidth(0),myHeight(0)
{
  stbi_set_flip_vertically_on_load(1);
  myLocalBuffer=stbi_load(path.c_str(), &myWidth, &myHeight, &myBPP,4);


  glGenTextures(1,&myRendererID);
  glBindTexture(GL_TEXTURE_2D,myRendererID);

  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);

  glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA8,myWidth,myHeight,0,GL_RGBA,GL_UNSIGNED_BYTE,myLocalBuffer);
  glBindTexture(GL_TEXTURE_2D,0);

  if(myLocalBuffer)
  {
    stbi_image_free(myLocalBuffer);
  }
}


Texture::~Texture()
{
   glDeleteTextures(1,&myRendererID);
}



void Texture::Bind(unsigned int slot)
{
  glActiveTexture(GL_TEXTURE0+slot);
  glBindTexture(GL_TEXTURE_2D,myRendererID);
}


void Texture::UnBind()
{
  glBindTexture(GL_TEXTURE_2D,0);
}